package org.javaboy.demo;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class Client05 {
    public static void main(String[] args) throws IOException {
        RestClient client = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
        RestClientTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
        ElasticsearchClient es = new ElasticsearchClient(transport);
        SearchResponse<Book> resp = es.search(s -> s.index("books").withJson(new StringReader("{\n" +
                "  \"aggs\": {\n" +
                "    \"book_count\": {\n" +
                "      \"terms\": {\n" +
                "        \"field\": \"publish\",\n" +
                "        \"size\": 3\n" +
                "      },\n" +
                "      \"aggs\": {\n" +
                "        \"book_avg\": {\n" +
                "          \"avg\": {\n" +
                "            \"field\": \"price\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    },\n" +
                "    \"avg_book\":{\n" +
                "      \"max_bucket\": {\n" +
                "        \"buckets_path\": \"book_count>book_avg\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}")), Book.class);
    }
}
